<%@ Page language="vb" CodeFile="activateLeaseContract.aspx.vb" Inherits="activateLeaseContract_aspx_vb" %>

<%
    vehicleID = Request.QueryString("vehicleID")
    If Len(vehicleID) > 0 Then 
        vehicleID = CLng(vehicleID)
    End IF
    If (Request.QueryString("do")) = Nothing Then
        dbOpen()
        RecSet = Conn.Execute("Select v.leaseStartDate, v.partnerID, v.priceListID, v.completed, p.allowInvoiceDurationPenalty, p.allowInvoiceDistancePenalty, (Select Count(vehicleID) From tblInvoiceContent Where vehicleID = v.vehicleID) As vehicleCount, (Select Count(vehicleID) From tblInvoiceCreditContent Where vehicleID = v.vehicleID) As creditCount From tblVehicle v INNER JOIN tblProduct p ON p.productID = v.productID Where v.vehicleID = " & vehicleID & "")
        If Not RecSet.EOF Then
            leaseStartDate = RecSet.Fields("leaseStartDate").Value
            partnerID = RecSet.Fields("partnerID").Value
            priceListID = RecSet.Fields("priceListID").Value
            completed = RecSet.Fields("completed").Value
            allowInvoiceDurationPenalty = RecSet.Fields("allowInvoiceDurationPenalty").Value
            allowInvoiceDistancePenalty = RecSet.Fields("allowInvoiceDistancePenalty").Value
            vehicleCount = RecSet.Fields("vehicleCount").Value
            creditCount = RecSet.Fields("creditCount").Value
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If CDbl(completed) = 1 Then
            Response.Redirect("editVehicle.aspx?vehicleID=" & vehicleID & "&subDo=%20-%20CAN%20NOT%20ACTIVATE")
        End If
        vehicleCount = CDbl(vehicleCount) - CDbl(creditCount)
        invoiceDate = Now()
        invoiceDate = Left(invoiceDate, 10)
        paymentTerm = 10
        expiryDate = DateAdd("d", paymentTerm, Convert.ToDateTime(invoiceDate))
        If CDbl(allowInvoiceDurationPenalty) = 1 Then
            dbOpen()
            RecSet = Conn.Execute("SELECT i.invoiceDurationPenaltyID As invoiceCreditID, i.invoiceAmount, i.uw FROM tblInvoiceDurationPenalty i WHERE i.vehicleID = " & vehicleID & "")
            If Not RecSet.EOF Then
                invoiceCreditID = RecSet.Fields("invoiceCreditID").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                uw = RecSet.Fields("uw").Value
                invoiceClaim = invoiceAmount
                invoiceText = "Credit invoice on invoice ID " & invoiceCreditID & " due to lease contract reactivation."
                Response.Write("Insert Into tblInvoiceCreditDurationPenalty (invoiceDurationPenaltyID, vehicleID, invoiceAmount, invoiceClaim, uw, invoiceDate, expiryDate, paymentTerm, invoiceText) Values(" & invoiceCreditID & "," & vehicleID & "," & invoiceAmount & "," & invoiceClaim & "," & uw & ",'" & invoiceDate & "','" & expiryDate & "'," & paymentTerm & ",'" & invoiceText & "')<br><br>")
                Conn.Execute("Insert Into tblInvoiceCreditDurationPenalty (invoiceDurationPenaltyID, vehicleID, invoiceAmount, invoiceClaim, uw, invoiceDate, expiryDate, paymentTerm, invoiceText) Values(" & invoiceCreditID & "," & vehicleID & "," & invoiceAmount & "," & invoiceClaim & "," & uw & ",'" & invoiceDate & "','" & expiryDate & "'," & paymentTerm & ",'" & invoiceText & "')")
            End If
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End If
        If CDbl(allowInvoiceDistancePenalty) = 1 Then
            dbOpen()
            RecSet = Conn.Execute("SELECT i.invoiceDistancePenaltyID As invoiceCreditID, i.invoiceAmount, i.uw FROM tblInvoiceDistancePenalty i WHERE i.vehicleID = " & vehicleID & "")
            If Not RecSet.EOF Then
                invoiceCreditID = RecSet.Fields("invoiceCreditID").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                uw = RecSet.Fields("uw").Value
                invoiceClaim = invoiceAmount
                invoiceText = "Credit invoice on invoice ID " & invoiceCreditID & " due to lease contract reactivation."
                Response.Write("Insert Into tblInvoiceCreditDistancePenalty (invoiceDistancePenaltyID, vehicleID, invoiceAmount, invoiceClaim, uw, invoiceDate, expiryDate, paymentTerm, invoiceText) Values(" & invoiceCreditID & "," & vehicleID & "," & invoiceAmount & "," & invoiceClaim & "," & uw & ",'" & invoiceDate & "','" & expiryDate & "'," & paymentTerm & ",'" & invoiceText & "')<br><br>")
                Conn.Execute("Insert Into tblInvoiceCreditDistancePenalty (invoiceDistancePenaltyID, vehicleID, invoiceAmount, invoiceClaim, uw, invoiceDate, expiryDate, paymentTerm, invoiceText) Values(" & invoiceCreditID & "," & vehicleID & "," & invoiceAmount & "," & invoiceClaim & "," & uw & ",'" & invoiceDate & "','" & expiryDate & "'," & paymentTerm & ",'" & invoiceText & "')")
            End If
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End If
        If CInt(vehicleCount) > 0 Then
            dbOpen()
            RecSet = Conn.Execute("Select Top 1 ic.leaseCurrentDate As lastLeaseDate From tblInvoiceContent ic Where ic.vehicleID = " & vehicleID & " And ic.vehicleID Not In (Select icc.vehicleID From tblInvoiceCreditContent icc Where icc.vehicleID = ic.vehicleID) Order By ic.leaseCurrentDate desc")
            If Not RecSet.EOF Then
                lastLeaseDate = RecSet.Fields("lastLeaseDate").Value
            End If
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End If
        Response.Write("leaseStartDate = " & leaseStartDate & "<br>")
        If IsDBNull(lastLeaseDate) = True Then
            checkFirstDay = Right(leaseStartDate, 2)
            If checkFirstDay = "01" Then
                leaseStartDate = leaseStartDate
            Else
                leaseStartDate = Left(leaseStartDate, 8)
                leaseStartDate = leaseStartDate & "01"
                leaseStartDate = DateAdd("m", 1, Convert.ToDateTime(leaseStartDate))
            End If
        Else
            leaseStartDate = lastLeaseDate
            leaseStartDate = DateAdd("m", 1, Convert.ToDateTime(leaseStartDate))
        End If
        Response.Write("leaseStartDate = " & leaseStartDate & "<br>")
        dbOpen()
        RecSet = Conn.Execute("Select Top 1 ic.leaseCurrentDate, ic.invoiceID From (tblInvoiceContent ic INNER JOIN tblInvoice i ON i.invoiceID = ic.InvoiceID) Where i.partnerID = " & partnerID & " Order by invoiceID desc")
        If Not RecSet.EOF Then
            leaseCurrentDate = RecSet.Fields("leaseCurrentDate").Value
            invoiceID = RecSet.Fields("invoiceID").Value
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("leaseCurrentDate = " & leaseCurrentDate & "<br>")
        leaseStartDateDiff = DateDiff("m", Convert.ToDateTime(leaseStartDate), Convert.ToDateTime(leaseCurrentDate))
        leaseCurrentDate = leaseStartDate
        Response.Write("<br>")
        dbOpen()
        For leaseStartDateDiffLoop = 0 To leaseStartDateDiff
            Response.Write("leaseCurrentDate = " & leaseCurrentDate & "<br>")
            Response.Write("Insert Into tblInvoiceContent (invoiceID, vehicleID, priceListID, leaseCurrentDate) Values(" & invoiceID & "," & vehicleID & "," & priceListID & ",'" & leaseCurrentDate & ")<br>")
            Conn.Execute("Insert Into tblInvoiceContent (invoiceID, vehicleID, priceListID, leaseCurrentDate) Values(" & invoiceID & "," & vehicleID & "," & priceListID & ",'" & leaseCurrentDate & "')")
            leaseCurrentDate = DateAdd("m", 1, Convert.ToDateTime(leaseCurrentDate))
        Next
        dbClose()
        Response.Write("<br>")
        dbOpen()
        Response.Write("Update tblVehicle Set active = 1 Where vehicleID = " & vehicleID & "<br>")
        Conn.Execute("Update tblVehicle Set active = 1, leaseEndDate = Null, leaseEndKms = NULL Where vehicleID = " & vehicleID & "")
        dbClose()
        Response.Redirect("editVehicle.aspx?vehicleID=" & vehicleID & "&subDo=%20-%20ACTIVATED")
    End If
%>
